package com.bp.hearthealth.pulse.lnapps.bp_utils

import android.annotation.TargetApi
import android.content.Context
import android.content.res.Configuration
import android.os.Build
import android.os.LocaleList
import android.text.TextUtils
import java.util.Locale


object MultiLanguageUtils {
    fun attachBaseContext(context: Context): Context {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            createConfigurationResources(context)
        } else {
            setConfiguration(context)
            context
        }
    }

    /**
     * 设置语言
     *
     * @param context
     */
    private fun setConfiguration(context: Context) {
        val appLocale = getAppLocale(context)

        //如果本地有语言信息，以本地为主，如果本地没有使用默认Locale
        var locale: Locale? = null
        val spLanguage: String? = SpUtils.getString(context, Constants.LOCALE_LANGUAGE)
        val spCountry: String? = SpUtils.getString(context, Constants.LOCALE_COUNTRY)
        locale = if (!TextUtils.isEmpty(spLanguage) && !TextUtils.isEmpty(spCountry)) {
            if (spLanguage?.let {
                    spCountry?.let { it1 ->
                        isSameLocal(
                            appLocale, it, it1
                        )
                    }
                } == true) {
                appLocale
            } else {
                Locale(spLanguage, spCountry)
            }
        } else {
            appLocale
        }
        val configuration: Configuration = context.resources.configuration
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale)
        } else {
            configuration.locale = locale
        }
        val resources = context.resources
        val dm = resources.displayMetrics
        resources.updateConfiguration(configuration, dm) //语言更换生效的代码!
    }

    @TargetApi(Build.VERSION_CODES.N)
    private fun createConfigurationResources(context: Context): Context {
        val resources = context.resources
        val configuration: Configuration = resources.configuration
        val appLocale = getAppLocale(context)

        //如果本地有语言信息，以本地为主，如果本地没有使用默认Locale
        var locale: Locale? = null
        val spLanguage: String? = SpUtils.getString(context, Constants.LOCALE_LANGUAGE)
        val spCountry: String? = SpUtils.getString(context, Constants.LOCALE_COUNTRY)
        locale = if (!TextUtils.isEmpty(spLanguage) && !TextUtils.isEmpty(spCountry)) {
            if (spLanguage?.let {
                    spCountry?.let { it1 ->
                        isSameLocal(
                            appLocale, it, it1
                        )
                    }
                } == true) {
                appLocale
            } else {
                Locale(spLanguage, spCountry)
            }
        } else {
            appLocale
        }
        configuration.setLocale(locale)
        configuration.setLocales(LocaleList(locale))
        return context.createConfigurationContext(configuration)
    }

    /**
     * 更改应用语言
     *
     * @param
     * @param locale      语言地区
     * @param persistence 是否持久化
     */
    fun changeAppLanguage(context: Context, locale: Locale, persistence: Boolean) {
        val resources = context.resources
        val metrics = resources.displayMetrics
        val configuration: Configuration = resources.configuration
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            configuration.setLocale(locale)
            configuration.setLocales(LocaleList(locale))
            context.createConfigurationContext(configuration)
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale)
        } else {
            configuration.locale = locale
        }
        resources.updateConfiguration(configuration, metrics)
        if (persistence) {
            saveLanguageSetting(context, locale)
        }
    }

    //保存多语言信息到sp中
    private fun saveLanguageSetting(context: Context, locale: Locale) {
        SpUtils.saveString(context, Constants.LOCALE_LANGUAGE, locale.language)
        SpUtils.saveString(context, Constants.LOCALE_COUNTRY, locale.country)
    }

    //获取本地应用的实际的多语言信息
    private fun getAppLocale(context: Context): Locale? {
        //获取应用语言
        val resources = context.resources
        val configuration: Configuration = resources.configuration
        var locale: Locale? = null
        locale = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            configuration.locales.get(0)
        } else {
            configuration.locale
        }
        return locale
    }

    //判断sp中和app中的多语言信息是否相同
    fun isSameWithSetting(context: Context): Boolean {
        val locale = getAppLocale(context)
        val language = locale!!.language
        val country = locale.country
        val spLanguage: String? = SpUtils.getString(Constants.LOCALE_LANGUAGE)
        val spCountry: String? = SpUtils.getString(Constants.LOCALE_COUNTRY)
        return language == spLanguage && country == spCountry
    }

    private fun isSameLocal(appLocale: Locale?, spLanguage: String, spCountry: String): Boolean {
        val appLanguage = appLocale!!.language
        val appCountry = appLocale.country
        return appLanguage == spLanguage && appCountry == spCountry
    }
}

